<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="车载机具编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入车载机具编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端状态" prop="state">
        <el-select clearable v-model="queryParams.state" placeholder="请选择">
          <el-option
            v-for="item in terminal_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单滞留状态" prop="orderState">
        <el-select clearable v-model="queryParams.orderState" placeholder="请选择">
          <el-option
            v-for="item in order_backlog_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['company:terminal:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="machineryList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="车载机具编号" align="center" prop="num" />
        <el-table-column label="终端厂商" align="center" prop="firmName" />
        <el-table-column label="当前软件版本" align="center" prop="version" />
        <el-table-column label="终端状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="terminal_status" :value="scope.row.state" />
          </template>
        </el-table-column>
        <el-table-column label="订单滞留状态" align="center" prop="orderState">
          <template #default="scope">
            <dict-tag :options="order_backlog_status" :value="scope.row.orderState" />
          </template>
        </el-table-column>
        <el-table-column label="订单滞留数" align="center" prop="orderNum" />
        <el-table-column label="最近心跳时间" align="center" prop="heartTime" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.heartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['company:terminal:edit']"></el-button>
            </el-tooltip>
            <template v-if="scope.row.state == 4 || scope.row.state == 5">
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['company:terminal:remove']"></el-button>
              </el-tooltip>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="showDialog" :title="dialogTitle">
      <edit-machinery 
        v-if="dialogType == 1"
        ref="editMachinery"
        @cancel="cancel"
        @confirm="confirm"
      />
      <upload-machinery 
        v-if="dialogType == 2"
        ref="uploadMachinery"
        @cancel="cancel"
        @confirm="confirm"
      />
    </Dialog>
  </div>
</template>

<script setup name="Machinery">
import { listMachinery, delMachinery } from "@/api/bus/machinery"
import EditMachinery from './edit.vue'
import UploadMachinery from './upload.vue'
import { nextTick } from "vue"

const editMachinery = ref()
const uploadMachinery = ref()
const { proxy } = getCurrentInstance()
const { terminal_company, order_backlog_status, terminal_status } = proxy.useDict('terminal_company', 'order_backlog_status', 'terminal_status')

const machineryList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogType = ref(0)
const dialogTitle = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    num: null,
    orderState: null,       // 订单滞留状态
    state: null,            // 终端状态
  }
})

const { queryParams } = toRefs(data)

/** 导入按钮操作 */
function handleImport() {
  showDialog.value = true
  dialogType.value = 2
  dialogTitle.value = '机具编号导入'
}

/** 查询车载机具列表 */
function getList() {
  loading.value = true
  listMachinery(queryParams.value).then(response => {
    machineryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogType.value = 0
  dialogTitle.value = ''
}

const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '添加车载机具'
  nextTick(() => {
    editMachinery.value.pageInit()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '修改车载机具'
  nextTick(() => {
    editMachinery.value.pageInit(_id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除该车载机具？').then(function() {
    return delMachinery(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('company/terminal/export', {
    ...queryParams.value
  }, `车载机具列表_${proxy.getFileImportName()}.xlsx`)
}

getList()
</script>
